﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using FT.TouchScreen.Terminal.Model;
using FT.DAL.Orm;
using FT.DAL;
using FT.TouchScreen.Terminal.Util;
using FT.TouchScreen.Terminal.Util.Impl;

namespace FT.TouchScreen.Terminal.Controls
{
    public partial class MapPanel : UserControl,IMapControl
    {
        public MapPanel()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;
            personNavigator = new DefaultPersonNavigator(this,this.personPic,Color.Red,5,20,1000);

        }

        private Image mapImageBack = null;
        private IPersonNavigator personNavigator = null;

        public void SetFloor(int floorid)
        {
            FloorEntity floor = SimpleOrmOperator.Query<FloorEntity>(floorid);
            if (floor != null)
            {
                this.mapImageBack = Image.FromFile(Application.StartupPath+"\\"+floor.BackImg);
                this.BackgroundImage = (Image)this.mapImageBack.Clone();
                this.Refresh();
                this.InitDotByFloor(floorid);
            }
        }

        public void InitDotByFloor(int floorid)
        {
            IDataAccess access = FT.DAL.DataAccessFactory.GetDataAccess();
            DataTable dt1 = access.SelectDataTable("SELECT id,name,x,y,floorid FROM navigate_floor_dotnodes where floorid="+floorid.ToString(), "tmp1");
            DotNode dnTemp=null;
            MinShortPathHelper.ht.Clear();
            for (int i = 0; i < dt1.Rows.Count; i++)
            {
                dnTemp = new DotNode(dt1.Rows[i]["name"].ToString());
                dnTemp.X = Convert.ToInt32(dt1.Rows[i]["x"].ToString());
                dnTemp.Y = Convert.ToInt32(dt1.Rows[i]["y"].ToString());
                dnTemp.FloorId = floorid;
                MinShortPathHelper.ht.Add(dnTemp.Name, dnTemp);
               
            }


            DataTable dt2 = access.SelectDataTable("SELECT id,dotidfrom,dotidto,len,floorid FROM navigate_floor_lines where floorid=" + floorid.ToString(), "tmp1");
            LineNode lineTemp = null;
           
            for (int i = 0; i < dt2.Rows.Count; i++)
            {

                dnTemp = MinShortPathHelper.ht[dt2.Rows[i]["dotidfrom"].ToString()] as DotNode;
                lineTemp = new LineNode(dt2.Rows[i]["dotidto"].ToString(), Convert.ToInt32(dt2.Rows[i]["len"].ToString()));
                dnTemp.Lines.Add(lineTemp);    
            }
        }

        private int ComputePathLen(string start, string end)
        {
            if (end == "") return -1;
            DotNode gnStart = MinShortPathHelper.ht[start] as DotNode;
            DotNode gnEnd = MinShortPathHelper.ht[end] as DotNode;
            if (gnStart == null || gnEnd == null)
            {
               // MessageBox.Show("开始节点或结尾节点不正确！");
                return -1;
            }
            gnStart.SetRank(null);
            gnStart.BFind = true;
            List<DotNode> arrGN = new List<DotNode>();
            arrGN.Add(gnStart);
            MinShortPathHelper.InitRank(arrGN);
            MinShortPathHelper.FindMin(arrGN, gnEnd);
            return gnEnd.MinDist;
        }


        #region IMapControl 成员


        public int Navigate(string start, string end)
        {

            if (this.personNavigator != null)
            {
               return  this.personNavigator.Navigate(start, end);
            }
            return 0;
            //tbContent.Text = gnEnd.MinDist.ToString() + "\r\n" + gnEnd.Path;//在界面显示结果
            //throw new NotImplementedException();
        }

        public void SetStartPostion(string locNode)
        {
            DotNode node = MinShortPathHelper.ht[locNode] as DotNode;
            if (node != null)
            {
                this.personNavigator.LocationPerson(node.X, node.Y);
            }
            this.Refresh();
        }

        public void ShowTypeLocation(string type)
        {
            throw new NotImplementedException();
        }

        public void SetRoadPen(Pen pen)
        {
            this.personNavigator.SetRoadPen(pen);
        }

        public int NavigateShop(string shop)
        {
            DataTable dt1 = FT.DAL.DataAccessFactory.GetDataAccess().SelectDataTable("SELECT shopid,dotnodeid,(select name from navigate_floor_dotnodes where id=dotnodeid) as dotnodename,name FROM navigate_shop_doors where shopid in (select id from navigate_shopes where name='" + shop + "')", "tmp1");
            //throw new NotImplementedException();
            if (dt1 != null&&dt1.Rows.Count>=1)
            {
                string start = System.Configuration.ConfigurationManager.AppSettings["StartDot"].ToString();
                string dot=string.Empty;
                int preLen=0;
                for (int i = 0; i < dt1.Rows.Count; i++)
                {
                    int tmp=this.ComputePathLen(start, dt1.Rows[i]["dotnodename"].ToString());
                    if (i == 0)
                    {
                        preLen = tmp;
                        dot = dt1.Rows[i]["dotnodename"].ToString();
                    }
                    else
                    {

                        if (tmp < preLen)
                        {
                            preLen = tmp;
                            dot = dt1.Rows[i]["dotnodename"].ToString();
                        }
                    }
                }
                return this.Navigate(start, dot);

            }
            return 0;
        }

        #endregion
    }
}
